﻿Public Class UCKhachHang
    Dim connect As New connect()
    Private Sub UCKhachHang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        rdbNam.Checked = True
    End Sub

    Private Sub rdbNam_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdbNam.CheckedChanged
        If (rdbNam.Checked) Then
            rdbNu.Checked = False
        Else
            rdbNu.Checked = True
        End If
    End Sub

    Private Sub rdbNu_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdbNu.CheckedChanged
        If (rdbNu.Checked) Then
            rdbNam.Checked = False
        Else
            rdbNam.Checked = True
        End If
    End Sub

    Private Function TaoMaKH() As String
        Dim maKH As String

        Dim yy As Integer = DateTime.Today.Year Mod 100
        Dim mm As Integer = DateTime.Today.Month

        Dim maDau As String = yy.ToString() + mm.ToString("00")
        Dim dt As DataTable = connect.GetData("select * from KHACHHANG where MAKH like '" + maDau + "%'")
        Dim stt As Integer = dt.Rows.Count() + 1
        Dim str_stt As String = stt.ToString("000")

        maKH = maDau + str_stt

        Dim check As Boolean = False
        While (check = False)
            Dim dt2 As DataTable = connect.GetData("select * from KHACHHANG where MAKH = '" + maKH + "'")
            If (dt2.Rows.Count() > 0) Then
                stt = stt + 1
                str_stt = stt.ToString("000")
                maKH = maDau + str_stt
                Dim dt3 As DataTable = connect.GetData("select * from KHACHHANG where MAKH='" + maKH + "'")
                If (dt3.Rows.Count() > 0) Then
                    check = False
                Else
                    check = True
                End If
            Else
                check = True
            End If
        End While

        Return maKH
    End Function
    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        Dim maKH As String = TaoMaKH()
        Dim tenKH As String = txtHoTen.Text.Trim()
        Dim ngay As DateTime = dtNgaySinh.Value
        Dim ngaySinh As String = ngay.Year().ToString("0000") + "-" + ngay.Month().ToString("00") + "-" + ngay.Day().ToString("00")

        Dim CMND As String = txtCMND.Text.Trim()
        Dim Email As String = txtEmail.Text.Trim()
        Dim diaChi As String = txtDiaChi.Text.Trim()
        Dim dienThoai As String = txtDienThoai.Text.Trim()
        Dim gioiTinh As String
        If (rdbNam.Checked) Then
            gioiTinh = rdbNam.Checked.ToString()
        Else
            gioiTinh = rdbNam.Checked.ToString()
        End If
        Dim biXoa As String = "False"
        Dim sql As String = "insert into KHACHHANG(MAKH,HOTEN,NGAYSINH,CMND,GIOITINH,DIENTHOAI,EMAIL,DIACHI,BIXOA) values('" + maKH + "',N'" + tenKH + "','" + ngaySinh + "','" + CMND + "','" + gioiTinh + "','" + dienThoai + "',N'" + Email + "',N'" + diaChi + "','" + biXoa + "')"
        connect.ExecSql(sql)
        MessageBox.Show("Thêm Thành công!")
        txtHoTen.Text = ""
        txtCMND.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        txtEmail.Text = ""
        dtNgaySinh.Text = ""
        rdbNam.Checked = True
    End Sub
End Class
